package com.jabo.douban.demo.presentation.viewmodel;

import com.jabo.douban.demo.model.entity.NewsBean;
import com.jabo.douban.demo.model.usecase.GetRecommendListUseCase;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class RecommendedViewModel extends ViewModel {

    private GetRecommendListUseCase mRecommendListUseCase;
    private MutableLiveData<NewsBean> mLiveData;
    private int mPage = 1;
    private int num = 20;

    public RecommendedViewModel() {
        mRecommendListUseCase = new GetRecommendListUseCase();
    }

    public MutableLiveData<NewsBean> getLiveData() {
        if (mLiveData == null) {
            mLiveData = new MutableLiveData<>();
        }
        return mLiveData;
    }

    public void start() {
        loadNewsBeanList();
    }

    public void pullLoadMore() {
        mPage++;
        loadNewsBeanList();
    }

    public void refreshLoad() {
        mPage = 1;
        loadNewsBeanList();
    }

    private void loadNewsBeanList() {
        Disposable subscribe = mRecommendListUseCase.execute(mPage, num).subscribe(new Consumer<NewsBean>() {
            @Override
            public void accept(NewsBean newsBean) throws Exception {
                mLiveData.postValue(newsBean);
            }
        });
    }

}
